package com.tushen.datacenter.ts.data;

import com.alibaba.fastjson.JSONObject;
import com.common.util.DateUtil;
import com.common.util.MyLoger;
import com.common.util.OkHttpUtil;
import com.common.util.Util;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

public class DataUtil {

	private static final String TAG = "DataUtil";
    //private static String mHost = "http://smart.gateway.c061d490d82d24bbca33ebbb750c110b5.cn-shenzhen.alicontainer.com";
    //private static String mHost = "http://test-gateway.deepthen.com"; //测试环境
    //private static String mHost = "http://smart-gateway.deepthen.com";  //图深正式
    private static String mHost = "http://smart-gateway.c-smart.cn";  //海宏正式

    public static JSONObject getOrgById(String orgId,String tanantId){

        try {

            MyLoger.d(TAG,"getOrgById orgId="+orgId+" tanantId="+tanantId);
            String token = getToken();
            if(Util.isEmpty(token)){
                MyLoger.e(TAG,"getOrgById getToken error..");
                return null;
            }
            HashMap<String,String> headerMap = new HashMap<String,String>();
            headerMap.put("Authorization","Bearer "+token);
            headerMap.put("tenantId",tanantId);

            String url = mHost+"/base/org/getById?id="+orgId;

            String jsonStr = OkHttpUtil.executeGet(url,headerMap,null);

            if(Util.isEmpty(jsonStr)){
                return null;
            }

            return JSONObject.parseObject(jsonStr);
        }catch (Exception e){
            return null;
        }
    }

    public static JSONObject getdata0(String orgId){

        try {
            String url = mHost+"/base/org/getById?id="+orgId;

            String jsonStr = OkHttpUtil.executeGet(url,getHeader(),null);

            if(Util.isEmpty(jsonStr)){
                return null;
            }

            return JSONObject.parseObject(jsonStr);
        }catch (Exception e){
            return null;
        }
    }

	public static JSONObject getdata1(){
		
		String url = mHost+"/labor/attendanceRecord/getLatest?orgId="+DataManager.PROJECT_ID;

		String jsonStr = OkHttpUtil.executeGet(url,getHeader(),null);

		if(Util.isEmpty(jsonStr)){
		    return null;
        }

		return JSONObject.parseObject(jsonStr);
	}
    public static JSONObject getdata2(){

        String url = mHost+"/labor/statistics/project/getInfoGroupByCompany?orgId="+DataManager.PROJECT_ID;
                //+"&date="+DateUtil.getYYYYMMDDStr();

        String jsonStr = OkHttpUtil.executeGet(url,getHeader(),null);

        if(Util.isEmpty(jsonStr)){
            return null;
        }

        return JSONObject.parseObject(jsonStr);
    }

    public static JSONObject getdata3() {

        try {

            String startDatetime = DateUtil.getDateZeroStr();
            String endDatetime = DateUtil.getNowDateStr();
            String url = mHost + "/labor/statistics/project/getInfo?orgId=" + DataManager.PROJECT_ID;
                    //+ "&startDatetime=" + startDatetime + "&endDatetime=" + endDatetime;

            String jsonStr = OkHttpUtil.executeGet(url, getHeader(), null);

            if (Util.isEmpty(jsonStr)) {
                return null;
            }

            return JSONObject.parseObject(jsonStr);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JSONObject getdata4(){

        String url = mHost+"/employee/statistics/project/getCountGroupByAge?orgId="+DataManager.PROJECT_ID;

        String jsonStr = OkHttpUtil.executeGet(url,getHeader(),null);

        if(Util.isEmpty(jsonStr)){
            return null;
        }

        return JSONObject.parseObject(jsonStr);
    }

    public static JSONObject getdata5(){

        String url = mHost+"/employee/statistics/project/getCountGroupBySex?orgId="+DataManager.PROJECT_ID;

        String jsonStr = OkHttpUtil.executeGet(url,getHeader(),null);

        if(Util.isEmpty(jsonStr)){
            return null;
        }

        return JSONObject.parseObject(jsonStr);
    }

    public static JSONObject getdata6(){

        String date = DateUtil.getYYYYMMDDStr();

        String url = mHost+"/labor/statistics/project/getInfoGroupByWorkType?orgId="+DataManager.PROJECT_ID;
               // +"&startDate="+date+"&endDate="+date;

        String jsonStr = OkHttpUtil.executeGet(url,getHeader(),null);

        if(Util.isEmpty(jsonStr)){
            return null;
        }

        return JSONObject.parseObject(jsonStr);
    }

    public static String getToken(){

        String url = mHost+ "/oauth/oauth/token?client_id=admin&client_secret=123456&grant_type=client_credentials";
        String jsonStr = OkHttpUtil.executePostParam(url,null,null);
        MyLoger.d(TAG,"getToken ret= "+jsonStr);
        if(Util.isEmpty(jsonStr)){
            return null;
        }
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        if (jsonObject == null || jsonObject.getInteger("code") == null  || jsonObject.getInteger("code") != 200) {
            return null;
        }
        return jsonObject.getJSONObject("data").getString("access_token");

    }
    private static HashMap<String,String> getHeader(){

        HashMap<String,String> headerMap = new HashMap<String,String>();
        headerMap.put("Authorization","Bearer "+DataManager.TOKEN);
        headerMap.put("tenantId",DataManager.TANANT_ID);
        return headerMap;

    }

}
